﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using Model;

namespace LogisticsDAL
{
    public class CostDAL
    {
        /// <summary>
        /// 核算成本页面
        /// </summary>
        /// <returns></returns>
        public DataTable CarriesASching(int pageIndex,int pageSize,out int Recout,int FindState,string RecoutTime,string EndRescoutTime)
        {
            string tableName = "View_CarriesCost";
            string Pramary = "CarriesID";
            string Filte = "(FinishedState=2 OR FinishedState=3) ";
            if (FindState != -1)
            {
                Filte = Filte + " and FinishedState='"+FindState+"'";
            }
            if(RecoutTime!="" || EndRescoutTime != "")
            {
                Filte = Filte + " and ReceiveDate>='" + RecoutTime + "' and ReceiveDate<='"+EndRescoutTime+"'";
            }
            DataTable table = PROC_Helper.Paging(tableName, Pramary, pageIndex, pageSize, Filte, out Recout);
            Seching sch = new Seching();
            
            return table;
        }


        public Seching GetCost(int schingID)
        {
            string sql = "SELECT  [SchedulingID],[FK_CarriersID],[OilCost],[Toll] ,[Fine] ,[OtherCost],[TotalCost],[FK_UserID] FROM [Scheduling] where SchedulingID=@SchedulingID";
            SqlParameter[] pram =
            {
                new SqlParameter("@SchedulingID",schingID)
            };
            SqlDataReader read = DBHelper.getExreaders(sql, pram);
            Seching sch = new Seching();
            if (read.HasRows)
            {
                while(read.Read())
                {
                    sch.SchedulingID = Convert.ToInt32(read["SchedulingID"]);
                    sch.OilCost = Convert.ToSingle(read["OilCost"]);
                    sch.OtherCost = Convert.ToSingle(read["OtherCost"]);
                    sch.Toll = Convert.ToSingle(read["Toll"]);
                    sch.Fine = Convert.ToSingle(read["Fine"]);
                }
            }
            return sch;
        }

        /// <summary>
        /// 结算成本
        /// </summary>
        /// <param name="Carrid"></param>
        /// <param name="OilCost"></param>
        /// <param name="Toll"></param>
        /// <param name="Fine"></param>
        /// <param name="OtherCost"></param>
        /// <returns></returns>
        public int Cost(float OilCost,float Toll,float Fine,float OtherCost,int Carrid)
        {
            string sqlprpc = "p_InsertCost";
            SqlParameter[] pram =
            {
                new SqlParameter("@OilCost",OilCost),
                new SqlParameter("@Toll",Toll),
                new SqlParameter("@Fine",Fine),
                new SqlParameter("@OtherCost",OtherCost),
                new SqlParameter("@Carrid",Carrid)
                
            };
            int num = DBHelper.Number(sqlprpc, pram);
            return num;
        }



        //修改
        public int Update(int SchedulingID, float OilCost, float Toll, float Fine, float OtherCost)
        {
            string sqlprpc = "P_UpdateCost";
            SqlParameter[] pram =
            {
                new SqlParameter("@SchedulingID",SchedulingID),
                new SqlParameter("@OilCost",OilCost),
                new SqlParameter("@Toll",Toll),
                new SqlParameter("@Fine",Fine),
                new SqlParameter("@OtherCost",OtherCost)
            };
            int num = DBHelper.Number(sqlprpc, pram);
            return num;
        }


        public SqlDataReader BindCostRead(int carrid)
        {
            string sql = "p_CostSelect";
            SqlParameter[] pram =
            {
                new SqlParameter("@Carrid",carrid)
            };
            SqlDataReader read = DBHelper.Exreader(sql, pram);
         
            return read;
        }

        public DataTable sqlMakeReport(string TeamName,DateTime RecDate,DateTime ReceDateEnd)
        {
            string sql = "p_MakeRepront";
            SqlParameter[] pram =
            {
                new SqlParameter("@TeamName",TeamName),
                new SqlParameter("@RecoutDate",RecDate),
                new SqlParameter("@ReceiveDateEND",ReceDateEnd)
            };
            DataTable tb = DBHelper.GetTables(sql, pram);
            return tb;
        }
    }
}
